import { Injectable } from '@angular/core';

import { Http } from '@angular/http';
import { Service } from '../../server/server';

interface loginResponse {
    result: string[];
    result_code:string;
    message:string;
    length:number;
  }

@Injectable()
export class LoginService {

    private baseUrl: string = '';

    constructor(private http: Http, private server: Service) {
        this.baseUrl = server.zsurl;
    }

    getVerify(): Promise<Array<any>> {
        let url = this.server.baseUrl + 'admin/pusers/getVerify?' + Math.random();
        return this.http
            .get(url)
            .toPromise()
            .then(res => {
                let r = res.json();
                return r;
            });
    }

    checkSum(param): Promise<Array<any>> {
        //发布模式
        let url = this.server.baseUrl + 'admin/pusers/login';
        return this.http.post(url, param)
            .toPromise()
            .then(res => {
                let r = res.json();
                //console.log(r)
                return r;
            });

        //开发模式显示全部权限
        // let url = "assets/login.json";
        // return this.http
        //     .get(url)
        //     .toPromise()
        //     .then(res => {
        //         console.log(res)
        //         let r = res.json();
        //         return r;
        //     });
    }
    
    processRights(list: Array<any>): any {
        let result: any = {};
        if (!list || !Array.isArray(list)) {
            return result;
        }
        for (let item of list) {
            let code: string = item.menuCode;
            let ps: string = item.permissonCode;
            result[code] = {};
            result[code]['permissions'] = ps;
            let arr: Array<string> = ps.split(/\,/g);
            for (let p of arr) {
                if (p != null && p.trim().length > 0) {
                    result[code][p.trim()] = true;
                }
            }
        }
        return result;
    }

}